<template>
    <div class="edu-page-warp">
        <condition-card @search="searchList" @reset="reset">
            <el-row>
                <el-col :span="8">
                    <el-form-item :label="$t('tutorAuth.entranceGrade')">
                        <!-- <GradeSelect v-model="queryParams.grade" :showAll="false"></GradeSelect> -->
                        <el-date-picker
                            style="width:100%"
                            v-model="queryParams.grade"
                            align="right"
                            type="year"
                            value-format="yyyy"
                            placeholder="请选择"
                            :clearable="true">
                        </el-date-picker>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item
                        :label="$t('tutorAuth.currentGrade')"
                    >
                        <!-- <GradeSelect v-model="queryParams.currentGrade" :showAll="false"></GradeSelect> -->
                        <el-date-picker
                            style="width:100%"
                            v-model="queryParams.currentGrade"
                            align="right"
                            type="year"
                            value-format="yyyy"
                            placeholder="请选择"
                            :clearable="true">
                        </el-date-picker>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item :label="$t('school.school')">
                        <DictSelect
                            v-model="queryParams.schoolId"
                            :data="options.school"
                            filterable
                        ></DictSelect>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item :label="$t('Parameter.schoolMajor')">
                        <SchoolMajorSelect
                            :schoolId="queryParams.schoolId"
                            v-model="queryParams.professionCode"
                        ></SchoolMajorSelect>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item :label="$t('tutorList.faculty')">
                        <DictSelect
                            v-model="queryParams.faculty"
                            :data="options.faculty"
                            filterable
                        ></DictSelect>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item :label="$t('tutorList.major')">
                        <!-- <MajorSelect :faculty="queryParams.faculty" v-model="queryParams.major"></MajorSelect> -->
                        <MyMajor
                            :faculty="queryParams.faculty"
                            :grade="queryParams.currentGrade"
                            v-model="queryParams.major"
                        ></MyMajor>
                    </el-form-item>
                </el-col>
            </el-row>
        </condition-card>
        <el-card shadow="never">
            <div class="edu-page-title">{{$t('tutorGuide.tutorChangeLog')}}</div>
            <div class="edu-opertion-bar">
                <div class="float-left">
                    <el-col :span="8">
                        <DictSelect
                            v-model="queryParams.selectType"
                            :data="options.selectType"
                            :clearable="false"
                        ></DictSelect>
                    </el-col>
                    <KeywordInput
                        v-model="queryParams.keyword"
                        @enter="searchList"
                        :placeholder="queryParams.selectType=='1'?'按学号或姓名搜索':'按工号或姓名搜索'"
                    ></KeywordInput>
                </div>
                <div class="float-right">
                    <el-button type="primary" @click="exportTutorLog">{{$t('message.export')}}</el-button>
                </div>
            </div>
            <edu-table
                url="/api/welcomeservice/tutorChangeLog/list"
                ref="mainTable"
                :order="false"
                :selection="true"
            >
                <el-table-column :label="$t('label.stuCode')" prop="studentId" fixed="left" width="100"/>
                <el-table-column :label="$t('label.stuName')" prop="studentName" fixed="left" width="100" show-overflow-tooltip/>
                <el-table-column :label="$t('label.sex')" prop="sexI18n" width="80"/>
                <el-table-column
                    :label="$t('label.college')"
                    prop="facultyI18n"
                    show-overflow-tooltip
                    width="160"
                >
                    <template slot-scope="scope">
                        <span v-if="scope.row.type== 2 ">{{scope.row.facultyI18n}}</span>
                    </template>
                </el-table-column>
                <el-table-column :label="$t('label.major')" prop="majorI18n" show-overflow-tooltip width="160">
                    <template slot-scope="scope">
                        <span v-if="scope.row.type== 2 ">{{scope.row.majorI18n}}</span>
                    </template>
                </el-table-column>
                <el-table-column :label="$t('school.school')" prop="school" show-overflow-tooltip width="80">
                    <template slot-scope="scope">
                        <span v-if="scope.row.type== 1 ">{{scope.row.school}}</span>
                    </template>
                </el-table-column>
                <el-table-column
                    :label="$t('Parameter.schoolMajor')"
                    prop="professionCodeI18n"
                    show-overflow-tooltip
                    width="140"
                >
                    <template slot-scope="scope">
                        <span v-if="scope.row.type== 1 ">{{scope.row.professionCodeI18n}}</span>
                    </template>
                </el-table-column>
                <el-table-column :label="$t('label.grade')" prop="currentGrade" width="80"/>
                <el-table-column :label="$t('tutorGuide.tutorType')" prop="type" width="100">
                    <template slot-scope="scope">
                        <span size="mini" v-if="scope.row.type== 1 ">新生导师</span>
                        <span type="success" v-if="scope.row.type== 2 " size="mini">学院导师</span>
                    </template>
                </el-table-column>
                <el-table-column :label="$t('Parameter.jobNumber')" prop="teacherCode" width="100"/>
                <el-table-column :label="$t('tutorGuide.tutorName')" prop="teacherName" width="120" show-overflow-tooltip/>
                <el-table-column
                    :label="$t('tutorManage.applyTime')"
                    prop="applyTime"
                    width="160"
                />
                <el-table-column
                    :label="$t('操作人')"
                    prop="operator"
                    width="140"
                    show-overflow-tooltip
                />
                <el-table-column
                    :label="$t('message.remark')"
                    prop="remark"
                    width="200"
                    show-overflow-tooltip
                />
            </edu-table>
        </el-card>
        <!-- 导出导师变更日志 -->
        <!-- <edu-export-dialog title="提示" @close="closeFreshEx" ref="downloadRef" :data="exportResult">
        </edu-export-dialog>-->
    </div>
</template>
<script>
import UTILS from "common/src/utils/utils";
import { queryDic } from "common/src/api/dictionary";
import BPIs from "common/src/api/welcomeService/schoolRelation";
import SchoolMajorSelect from "./component/SchoolMajorSelect";
import MyMajor from "./component/MyMajor";
import APIs from "common/src/api/welcomeService/tutorExportImport";

// 本科生导师变更日志
export default {
    name: "tutorChangeLog",
    computed: {},
    components: {
        SchoolMajorSelect,
        MyMajor
    },
    mounted() {
        this.getOptions();
    },
    watch: {
        "queryParams.currentGrade": {
            handler: function(newVal, oldVal) {
                if (this.queryParams.schoolId) {
                    this.queryParams.schoolId = null;
                }
                this.options.school = [];
                if (newVal) {
                    this.querySchool.condition.grade = newVal;
                    this.findSchool();
                }
            }
        }
    },
    data() {
        return {
            queryParams: {
                grade: null,
                currentGrade: null,
                schoolId: null,
                major: "",
                professionCode: "",
                faculty: "",
                selectType: "1",
                keyword: "",
                stuCondition: "",
                tutorCondition: ""
            },
            querySchool: {
                pageNum_: 0,
                pageSize_: 0,
                condition: {}
            },
            selectionDatas: [],
            options: {
                selectType: [
                    {
                        label: this.$t("tutorManage.searchByStudent"),
                        value: "1"
                    },
                    { label: this.$t("tutorManage.searchByTutor"), value: "2" }
                ],
                faculty: [],
                school: []
            }
        };
    },
    methods: {
        getOptions() {
            //筛选条件字典查询
            let data = {
                lang: this.$store.getters.language || "cn",
                type: "allChild",
                keys: ["X_YX"]
            };
            queryDic(data).then(res => {
                UTILS.fillSelect(
                    {
                        faculty: "X_YX"
                    },
                    res.data,
                    this.options
                );
            });
            this.searchList();
        },
        findSchool() {
            //学堂下拉
            BPIs.findSchool(this.querySchool).then(res => {
                if (res.code == 200 && res.data.list.length > 0) {
                    for (const item of res.data.list) {
                        this.options.school.push({
                            label: item.name,
                            value: item.id
                        });
                    }
                }
            });
        },
        reset() {
            for (const key in this.queryParams) {
                if (key != "selectType") this.queryParams[key] = "";
            }
            this.searchList();
        },
        searchList() {
            this.queryParams.stuCondition = "";
            this.queryParams.tutorCondition = "";
            if (this.queryParams.selectType == 1) {
                //学生
                this.queryParams.stuCondition = this.queryParams.keyword;
            } else {
                this.queryParams.tutorCondition = this.queryParams.keyword;
            }
            this.$refs.mainTable.search(this.queryParams);
        },
        exportTutorLog() {
            let loading = this.$loading({
                lock: true,
                text: "Loading",
                spinner: "el-icon-loading",
                background: "rgba(255, 255, 255, .48)"
            });
            this.queryParams.stuCondition = "";
            this.queryParams.tutorCondition = "";
            if (this.queryParams.selectType == 1) {
                //学生
                this.queryParams.stuCondition = this.queryParams.keyword;
            } else {
                this.queryParams.tutorCondition = this.queryParams.keyword;
            }
            let data = _.cloneDeep(this.queryParams);
            data.ids = this.$refs.mainTable.getCheckData().map(c => c.id);
            APIs.exportTutorLog(data).then(res => {
                if (res.code == 200) {
                    this.delayRequest(res.data.key, loading);
                } else {
                    loading.close();
                }
            });
        },
        async delayRequest(key, loading) {
            await new Promise(resolve => setTimeout(resolve, 3000));
            APIs.getReportStatus(key)
                .then(res => {
                    if (res.code == 200 && res.data.status) {
                        let path = APIs.exportLogUrl + res.data.path;
                        window.open(path);
                        loading.close();
                    } else {
                        this.delayRequest(key, loading);
                    }
                })
                .catch(ex => {
                    this.delayRequest(key, loading);
                });
        }
    }
};
</script>
<style lang="scss" >
</style>
